package com.card.notice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.card.common.MessageComm;
import com.card.user.User;
import com.card.user.UserManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.FilterOperator;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.Query.FilterPredicate;
import com.xiaoao.net.json.message.JsonMessage;
import com.xiaoao.net.utils.EntityUtils;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.URLUtil;
import com.xiaoao.net.utils.json.GSONUtils;

/**
 * 公告系统统一管理类
 * @author Administrator
 *
 */
public class NoticeManager{
	private static final NoticeManager instance = new NoticeManager();
	public static NoticeManager getInstance() {
		return instance;
	}
	
	public NoticeManager() {
		init();
	}
	
	public static final String T_NOTICE_DATA = "noticedata";
	/**
	 * 公告集合
	 */
	private List<GlobNotice> noticeList = new ArrayList<>();
	
	/**
	 * 初始化,每天0点重新加载一次
	 */
	public void init(){
		List<GlobNotice> list = getNoticeFromDB();
		noticeList.clear();
		for(GlobNotice notice:list){
			noticeList.add(notice);
		}
		if(noticeList.isEmpty()) {
			for (int i = 0; i < 3; i++) {
				testAddToDB();
			}
		}
	}
	
	public void addNewNotice(String noticeJson){
		String notices = null;
		notices = URLUtil.decodeUTF8(noticeJson);
		if(notices != null){
			Gson gson = new GsonBuilder()
					.setDateFormat(GSONUtils.DATEFORMAT)
//					.registerTypeAdapter(Base.class,Base.CreateGsonAdapter())
					.disableHtmlEscaping()
					.create();
			String newNoticeJson = notices.replace("*", " ");
			GlobNotice notice = gson.fromJson(newNoticeJson, GlobNotice.class);
			notice.updateNoticeId();
			addNewNotice(notice);
		}
	}
	
	/**
	 * 添加新公告
	 * @param notice
	 */
	public void addNewNotice(GlobNotice notice){
		if(notice.isFit()){
			JsonMessage serverMsg = new JsonMessage(MessageComm.S2C_AddNotice);
			serverMsg.addProperty("notice", notice);
			UserManager.getInstance().broadMessage(serverMsg);
		}
		noticeList.add(notice);
		//添加到数据库
		 notice.saveDB();
	}
	
	public List<GlobNotice> getNoticeFromDB(){
		List<GlobNotice> dbList = new ArrayList<>();
		//从数据库获取玩家数据
		Query query = new Query(T_NOTICE_DATA);
		query.addFilter(new FilterPredicate(GlobNotice.ENDDATE, FilterOperator.GREATER_THAN, (long)new Date().getTime()));
		query.setLimit(10000);
		List<Entity> noticeEntityList = DBManager.getInstance().queryEntity(query);
		for(Entity entity:noticeEntityList){
			GlobNotice notice = new GlobNotice(entity);
			dbList.add(notice);
		}
		return dbList;
	}
	
	/**
	 * 从数据库中删除公告
	 * @param id
	 */
	public void removeNoticeFromDB(String id){
		for (int i = noticeList.size()-1; i >= 0; i--) {
			if(StringUtil.equals(id, noticeList.get(i).getNoticeId())) {
				noticeList.remove(i);
				break;
			}
		}
		EntityUtils.Remove(T_NOTICE_DATA,id);
	}
	
	private void testAddToDB(){
		GlobNotice notice = new GlobNotice("testNotice", new Date(),  new Date(117, 7, 24));
		notice.saveDB();
	}
	
	/**
	 * 获取公告
	 * @return
	 */
	public List<GlobNotice> getNotices(){
		List<GlobNotice> list = new ArrayList<>();
		for(GlobNotice notice : noticeList){
			if(notice.isFit()){
				list.add(notice);
			}
		}
		return list;
	}
	
	/**
	 * 获取公告
	 * @return json类型
	 */
	public String getNoticesJson(){
		List<GlobNotice> list = new ArrayList<>();
		for(GlobNotice notice : getNoticeFromDB()){
			list.add(notice);
		}
		
		Gson gson = new GsonBuilder()
				.setDateFormat(GSONUtils.DATEFORMAT)
				.disableHtmlEscaping()
				.create();
		return gson.toJson(list);
	}
	
	/**
	 * 推送公告列表给玩家
	 * @param user
	 */
	public void sendNotice2User(User user) {
//		ServerMsg serverMsg = MessageFactory.getInstance().getServerMsg();
//		serverMsg.appendMsgId(CommNum.MSG_S2C_NOTICE_LIST);
//		fillMsg(serverMsg);
//		MessageCenter.getInstance().sendMsg(serverMsg, user.getCtx());
	}
	
	public static void main(String[] args) {
		NoticeManager.getInstance().testAddToDB();
	}
}
